package com.baj.ldap.controller;

import com.baj.ldap.entry.Person;
import com.baj.ldap.service.PersonService;
import com.baj.ldap.util.LdapResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.naming.InvalidNameException;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private PersonService personService;

    @GetMapping(value = "/findOne")
    public LdapResult findOne(String key, String value) {
        LdapResult result = personService.findOne(key, value);
        return result;
    }

    @GetMapping(value = "/findByCn")
    public LdapResult findByCn(String cn) {
        LdapResult result = personService.findByCn(cn);
        return result;
    }

    @GetMapping(value = "/findAll")
    public LdapResult findAll() {
        LdapResult result = personService.listAllPerson();
        return result;
    }

    @GetMapping(value = "/findList")
    public LdapResult findList(String key, String value) {
        LdapResult result = personService.findList(key, value);
        return result;
    }

    @PostMapping(value = "/create")
    public LdapResult create(@RequestParam(name = "cn") String cn, @RequestParam(name = "sn") String sn, @RequestParam(name = "userPassword") String userPassworld) {
        Person person = new Person();
        return personService.save(person);
    }

    @PostMapping(value = "/update")
    public LdapResult update() throws InvalidNameException {
        LdapResult result = personService.findOne("cn","尚家朋");
        Person person = (Person) result.getData();
        person.setName("哈哈哈哈");


        return personService.modifyPerson(person);
    }

    @PostMapping(value = "/delete")
    public LdapResult delete(@RequestParam(name = "cn") String cn) {
        Person person = new Person();
        return personService.deletePerson(person);
    }

}
